from ShapeAndRasterToolBox import UFP
from remoteserverssh import SSHConnection as sshc
from Sentinel2Raster import Sentinel2, ChinaSentinel2, ShapeFile, RasterFile, ShapeFileToRasterFile, ShapeToRasterFillFieldValue
import Sentinel2Raster
from Sentinel2Raster import ShapeFileTransformSpatialreferencesystem
import os
import json
import shutil
import re
from tqdm import tqdm
import pathlib
from ToolBox import TL, FTB


def task_1():
    """
    批量压缩文件夹
    :return:
    """
    sourcedir = r"G:\2020年河北影像"
    saveunzipfiledir = r"F:\河北2020秋季Sentinel2"
    subfolderpathlist = UFP.GetSubDir(sourcedir)
    subfilepathlist = UFP.GetSubFile(sourcedir)
    subpathlist = subfolderpathlist + subfilepathlist
    print("正在压缩文件...")
    for subpath in tqdm(subpathlist):
        UFP.ZipFile(subpath, saveunzipfiledir)
    print("压缩完成!")
    return


def task_2():
    """
    建立远程服务器链接
    :return:
    """
    host = "192.168.1.73"
    port = 12227
    username = "root"
    password = "root"
    remoteserver = sshc(host, port, username, password)
    localdir = pathlib.PureWindowsPath(r"E:\迅雷下载")
    remotedir = pathlib.PurePosixPath("/home/fyy_dapeng/test_TMP/test2")
    localfilepathlist = UFP.GetGoalFile(str(localdir), ".zip")
    print("正在上传文件...")
    for localfilepath in tqdm(localfilepathlist):
        localfilename = os.path.split(localfilepath)[1]
        linuxfilepathobject = pathlib.PurePosixPath(remotedir, localfilename)
        linuxfilepath = str(linuxfilepathobject)
        remoteserver.Upload(localfilepath, linuxfilepath)
    remoteserver.close()
    print("完成上传!")
    return


def task_3():
    """
    批量压缩与上传
    :return:
    """
    task_1()
    task_2()
    return


def task_4():
    """
    上传文件夹
    :return:
    """
    host = "192.168.1.73"
    port = 12227
    username = "root"
    password = "root"
    remoteserver = sshc(host, port, username, password)
    localdir = r"E:\猪体长\郭大素81头育肥1.26"
    remotedir = "/home/fyy_dapeng/test_TMP/test2"
    remoteserver.UploadFolder(localdir, remotedir)
    remoteserver.close()
    return


def task_5():
    host = "192.168.1.73"
    port = 12227
    username = "root"
    password = "root"
    remoteserver = sshc(host, port, username, password)
    localdir = "F:\训练数据集"
    remotedir = "/home/fyy_dapeng/resultdata/Goolge_cangzhou_10161502"
    getfoldernamecommand = "ls" + " " + remotedir
    pathsstr = remoteserver.exec_command(getfoldernamecommand)
    filefolderpathlist = pathsstr.split("\n")[48:-1]
    for i in filefolderpathlist:
        subfolderpathremote = pathlib.PurePosixPath(remotedir, i)
        subfolderpathlocal = pathlib.PureWindowsPath(localdir, i)
        print(i)
        if not os.path.exists(subfolderpathlocal):
            os.makedirs(subfolderpathlocal)
        findfilenamecommand = "ls " + str(subfolderpathremote)
        pathsstr = remoteserver.exec_command(findfilenamecommand)
        subfilenamelist = pathsstr.split("\n")[:-1]
        for subfilename in subfilenamelist:
            print(subfilename)
            remotesubfilepathobject = pathlib.PurePosixPath(subfolderpathremote, subfilename)
            localsubfilepathobject = pathlib.PureWindowsPath(subfolderpathlocal, subfilename)
            remotesubfilepath = str(remotesubfilepathobject)
            localsubfilepath = str(localsubfilepathobject)
            remoteserver.Download(remotesubfilepath, localsubfilepath)
    return


def task_6():
    """
    筛选存在json文件的训练数据集
    :return:
    """
    datasetdir = "E:\Yang\二次筛选"
    savedir = "E:\Yang\三次筛选"
    datasetfolderpathlist = UFP.GetSubDir(datasetdir)
    for datasetfolderpath in datasetfolderpathlist:
        datasetfoldername = os.path.split(datasetfolderpath)[1]
        newdatasetfolderpath = os.path.join(savedir, datasetfoldername)
        if not os.path.exists(newdatasetfolderpath):
            os.makedirs(newdatasetfolderpath)
        jsonfilepathlist = UFP.GetGoalFile(datasetfolderpath, ".json")
        for jsonfilepath in jsonfilepathlist:
            jsonfilename = os.path.split(jsonfilepath)[1]
            newjsonfilepath = os.path.join(newdatasetfolderpath, jsonfilename)
            jpgfilepath = jsonfilepath.replace(".json", ".jpg")
            jpgfilename = os.path.split(jpgfilepath)[1]
            newjpgfilepath = os.path.join(newdatasetfolderpath, jpgfilename)
            shutil.copyfile(jsonfilepath, newjsonfilepath)
            shutil.copyfile(jpgfilepath, newjpgfilepath)
    print("end!")
    return


def task_7():
    """
    创建目录
    :return:
    """
    datasetdir = "F:\筛选后的训练数据集"
    savedir = "D:\c盘文件勿删！\桌面\贾冰\漏检大棚"
    datasetfolderpathlist = UFP.GetSubDir(datasetdir)
    for datasetfolderpath in datasetfolderpathlist:
        datasetfoldername = os.path.split(datasetfolderpath)[1]
        tempdatasetfoldername = datasetfoldername.split("_")[0]
        newdatasetfoldername = tempdatasetfoldername.replace("J50E0", "漏检大棚_")
        newdatasetfolderpath = os.path.join(savedir, newdatasetfoldername)
        if not os.path.exists(newdatasetfolderpath):
            os.makedirs(newdatasetfolderpath)
    return


def task_8():
    """
    筛选存在json文件的训练数据集
    :return:
    """
    filterdatasetdir = "E:\Yang\三次筛选"
    sourcedatasetdir = "G:\洋洋_检查数据"
    savedir = "E:\Yang\四次筛选"

    datasetfoldernamelist = os.listdir(sourcedatasetdir)

    for datasetfoldername in tqdm(datasetfoldernamelist):

        filterdatasetfolderpath = os.path.join(filterdatasetdir, datasetfoldername)

        sourcedatasetfolderpath = os.path.join(sourcedatasetdir, datasetfoldername)

        savedatasetfolderpath = os.path.join(savedir, datasetfoldername)

        os.makedirs(savedatasetfolderpath)

        sourcedatasetjsonfilepathlist = UFP.GetGoalFile(sourcedatasetfolderpath, r".json")

        for sourcedatasetjsonfilepath in sourcedatasetjsonfilepathlist:

            jsonfilename = os.path.split(sourcedatasetjsonfilepath)[1]

            filterdatasetjsonfilepath = os.path.join(filterdatasetfolderpath, jsonfilename)

            if not os.path.exists(filterdatasetjsonfilepath):
                os.remove(sourcedatasetjsonfilepath)
            else:
                os.remove(sourcedatasetjsonfilepath)

                shutil.copyfile(filterdatasetjsonfilepath, sourcedatasetjsonfilepath)

    print("end!")
    return


def task_9():
    """
    下载远程服务器文件
    :return:
    """
    remoteserverdir = "/home/DL_dataset/2020.9.30_dapeng/new_data"
    localhostdir = "E:\\贾冰\\11021102"
    host = "192.168.1.73"
    port = 22
    username = "root"
    password = "DELL123dell"
    newremoteserver = sshc(host, port, username, password)
    command = "ls -A " + remoteserverdir
    result = newremoteserver.exec_command(command)
    remotefilenamelist = result.split("\n")
    tasklist = []
    for remotefilename in remotefilenamelist:
        remotesubfilepathobject = pathlib.PurePosixPath(remoteserverdir, remotefilename)
        linuxfilepath = str(remotesubfilepathobject)
        localhostfilepath = pathlib.PureWindowsPath(localhostdir, remotefilename)
        windowsfilepath = str(localhostfilepath)
        tasklist.append([linuxfilepath, windowsfilepath])
    newremoteserver.close()
    # newremoteserver = sshc(host, port, username, password)
    # for task in tqdm(tasklist):
    #     [linuxfilepath, windowsfilepath] = task
    #     filename = os.path.split(windowsfilepath)[1]
    #     newresult = newremoteserver.exec_command("find %s name %s" % (remoteserverdir, filename))
    #     print(newresult)
    # newremoteserver.close()
    newremoteserver = sshc(host, port, username, password)
    for task in tqdm(tasklist):
        [linuxfilepath, windowsfilepath] = task
        newremoteserver.Download(linuxfilepath, windowsfilepath)
    newremoteserver.close()
    return


def task_10():
    folderpath = "E:\download\木兰县测产点照片1"
    savedir = "E:\download\新建文件夹"
    filepathlist = UFP.GetGoalFile(folderpath, ".jpg")
    for filepath in tqdm(filepathlist):
        filename = os.path.split(filepath)[1]
        newfilepath = os.path.join(savedir, filename)
        shutil.copyfile(filepath, newfilepath)
    return


def task_11():
    """
    提取文件夹下所有文件名，提取关键信息，并生成相应的csv文件
    :return: 
    """
    folderpath = "E:\\张理想\\11月3日(1)"
    savefilepath = "E:\\张理想\\坐标.csv"
    filenamelist = os.listdir(folderpath)
    header = ["名称", "经度", "纬度"]
    infolist = []
    for jpgfilename in tqdm(filenamelist):
        filename = os.path.splitext(jpgfilename)[0]
        filestrlist = filename.split("_")
        gpsstrinfo = filestrlist[2:4]
        gpsinfo = [jpgfilename, float(gpsstrinfo[0]), float(gpsstrinfo[1])]
        infolist.append(gpsinfo)
    TL.WriteCSVFile(savefilepath, header, infolist)
    return


def tas_11():
    shapefilepath = "E:\localhosttestdata\中国区域哨兵网格号分布"
    hebeicountyshapefilepath = "E:\localhosttestdata\河北县域"
    layername = None
    savedirectory = "E:\localhosttestdata"
    jsonfilename = "test.json"
    jsonfilepath = os.path.join(savedirectory, jsonfilename)
    shapefilename = os.path.split(shapefilepath)[1]
    countyname = input("输入县名称")
    starttimestr = input("输入搜索影像起始时间")
    endtimestr = input("输入搜索影像结束时间")
    filename = countyname
    geojsonfilename = filename + ".geojson"
    tesgeojsonfilepath = "E:\\download\\map (1).geojson"
    geojsonfilepath = os.path.join(savedirectory, geojsonfilename)
    # featurejsonlist = TL.ShapeFileToJsonList(hebeicountyshapefilepath, layername)
    # featuredictlist = [json.loads(featurejson) for featurejson in tqdm(featurejsonlist)]
    # TL.DataWriteJsonFile(featuredictlist, jsonfilepath)
    # data = TL.JsonFileReadData(jsonfilepath)
    # for item in data:
    #     fielddict = item["properties"]
    #     qhmcfield = fielddict["qhmc"]
    #     if qhmcfield == countyname:
    #         print(item["geometry"])
    #         print(fielddict["qhmc"])
    # 
    # 
    # print(0)

    # starttime = [int(starttimestr[:4]), int(starttimestr[4:6]), int(starttimestr[6:])]
    # endtime = [int(endtime[:4]), int(endtime[4:6]), int(endtime[6:])]
    ftb = FTB()
    # ftb.DownloadSentinel2ByCountyNumber(jsonfilepath, geojsonfilepath, countyname, starttimestr, endtimestr)
    ftb.Sentinel2Download(tesgeojsonfilepath, starttimestr, endtimestr)
    return


def ProcessPool(argslist, taskfunction, processcount=4):
    """
    用于多进程加速
    :param argslist:gu
    :param taskfunction:
    :param processcount:
    :return:
    """
    print("start!")
    pool = Pool(processcount)
    pool.map(taskfunction, argslist)
    pool.close()
    pool.join()
    print("end！")
    return


def MoveJpgJsonfile():
    """
    检查文件夹中是否存在单独的jpg文件以及json文件，如果存在移动到新文件夹中
    :return:
    """
    datasetdirectory = "E:\\Yang\\testdata\\筛选样本二"
    savedatasetdirectory = "E:\\Yang\\testdata\\newdataset0934"
    jpgfilepathlist = TL.GetGoalFile(datasetdirectory, ".jpg")
    for jpgfilepath in tqdm(jpgfilepathlist):
        jpgfilename = os.path.split(jpgfilepath)[1]
        filename = os.path.splitext(jpgfilename)[0]
        jsonfilepath = os.path.join(datasetdirectory, filename + ".json")
        if os.path.exists(jsonfilepath):
            newjpgfilepath = os.path.join(savedatasetdirectory, jpgfilename)
            newjsonfilepath = os.path.join(savedatasetdirectory, filename + ".json")
            shutil.move(jpgfilepath, newjpgfilepath)
            shutil.move(jsonfilepath, newjsonfilepath)
    return


def task_12():
    datasetdirectory = r"E:\Yang\testdata\筛选样本二"
    sourcedatasedirectory = r"E:\Yang\202011110932\冯洋洋筛选样本"
    destination = r"E:\Yang\testdata\dataset202011110945"
    jpgfilepathlist = TL.GetGoalFile(datasetdirectory)
    for jpgfilepath in tqdm(jpgfilepathlist):
        jpgfilename = os.path.split(jpgfilepath)[1]
        filename = os.path.splitext(jpgfilename)[0]
        sourcejsonfilepath = os.path.join(sourcedatasedirectory, filename + r".json")
        destinationjsonfilepath = os.path.join(destination, filename + r".json")
        destinationjpgfilepath = os.path.join(destination, jpgfilename)
        shutil.copyfile(jpgfilepath, destinationjpgfilepath)
        shutil.copyfile(sourcejsonfilepath, destinationjsonfilepath)
    return


def task_13():
    zipfiledirectory = "E:\localhosttestdata\sentinel2zipfile\S2B_MSIL1C_20200904T030549_N0209_R075_T50SKC_20200904T045859.zip"
    savedirectory = "E:\localhosttestdata\sentinel2raster"
    Sentinel2.Sentinel2ZipfileToRasterfile(zipfiledirectory, savedirectory)
    return


def task_14():
    shapefilepath = "E:\张理想\MGRS_100kmSQ_ID_shp"
    layernamelist = Sentinel2.GetShapeFileLayerList(shapefilepath)
    zonenamelist = []
    for layername in layernamelist:
        layernamestrlist = layername.split("_")
        zonename = layernamestrlist[-1]
        zonenamelist.append(zonename)
        print(zonename)
    return


def task_15():
    # shapefilepath = "E:\\localhosttestdata\\xy"
    shapefilepath = r"E:\localhosttestdata\MGRS_100kmSQ_ID_shp"
    spatialreferencesystemshapefilepath = r"E:\localhosttestdata\xy"
    newshapefilepath = r"E:\localhosttestdata\newworldshapefile"
    if os.path.exists(newshapefilepath):
        shutil.rmtree(newshapefilepath)
    # gdafilepath = r"E:\张理想\MGRS_100kmSQ_ID.gdb"
    # ShapeFile.OpenGDBFile(gdafilepath)
    
    ChinaSentinel2.TransFormWorldSentinel2ZoneShapeFile(
        shapefilepath, spatialreferencesystemshapefilepath, newshapefilepath)
    return


def task_16():
    rasternametxtfilepath = r"E:\localhosttestdata\新建文本文档.txt"
    localhostrasterdirctory = r"G:\河南省YX\河南三月L2A"
    with open(rasternametxtfilepath, "r") as textfile:
        linecontent = textfile.read().splitlines()
    remoterasterfilenamelist = linecontent
    localhostrasterfilepathlist = TL.GetGoalFile(localhostrasterdirctory, ".tif")
    localhostrasterfilenamelist = []
    for localhostrasterfilepath in localhostrasterfilepathlist:
        localhostrasterfilename = os.path.split(localhostrasterfilepath)[1]
        localhostrasterfilenamelist.append(localhostrasterfilename)
        for remoterasterfilename in remoterasterfilenamelist:
            if remoterasterfilename == localhostrasterfilename:
                break
            elif remoterasterfilename != localhostrasterfilename and remoterasterfilename == remoterasterfilenamelist[-1]:
                print(remoterasterfilename, "不存在！")
            else:
                pass
    return


def task_17():
    rasterzipfiledirectory = r"G:\河北省YX\河北原始影像"
    saverasterfiledirectory = r"E:\丁振森\新建文件夹"
    rasterzipfilepathlist = TL.GetGoalFile(rasterzipfiledirectory, ".zip")
    for rasterzipfilepath in rasterzipfilepathlist:
        rasterzipfilename = os.path.split(rasterzipfilepath)[1]
        filename = os.path.splitext(rasterzipfilename)[0]
        pattern = re.compile(r"T50SPJ.*_201911.*")
        if pattern.search(filename):
            newrasterzipfilepath = os.path.join(saverasterfiledirectory, rasterzipfilename)
            shutil.copyfile(rasterzipfilepath, newrasterzipfilepath)
            print(rasterzipfilepath)
    return


def task_18():
    worldsentinel2shapefilepath = r"E:\localhosttestdata\newworldshapefile"
    hebeishapefilepath = r"E:\localhosttestdata\河北县域"
    newshapefilepath = r"E:\localhosttestdata\heibeicountysentinel2"
    newlayername = r"hebeicountysentinel2"
    ChinaSentinel2.MultipleLayerCombination(
        worldsentinel2shapefilepath, hebeishapefilepath, newshapefilepath, newlayername
    )
    return


def task_19():
    worldsentinel2shapefilepath = r"E:\localhosttestdata\newworldshapefile"
    countyshapefilepath = r"E:\localhosttestdata\中国省级行政区划_shp"
    newshapefilepath = r"E:\localhosttestdata\chinacountysentinel2"
    newlayername = r"hebeicountysentinel2"
    ChinaSentinel2.MultipleLayerCombination(
        worldsentinel2shapefilepath, countyshapefilepath, newshapefilepath, newlayername)
    return


def task_20():
    # sentinel2zipfilepath = r""
    # unzipdircetory = r"E:\贾冰\heibeisentinel2raster"
    # sentinel2dircetory = r"F:\河北省冬小麦自动解译数据"
    # sentinel2zipfilepathlist = TL.GetGoalFile(sentinel2dircetory, r".zip")
    # for sentinel2rasterfilepath in sentinel2zipfilepathlist:
    #     Sentinel2.Sentinel2SingleBandRaster(sentinel2rasterfilepath, unzipdircetory)
    rasterfileapth = r"E:\贾冰\sentinel2rastertrain\S2A_MSIL1C_T50SKH_20191114\S2A_MSIL1C_T50SKH_20191114_B01.tif"
    shapefilepath = r"F:\河北省冬小麦自动解译数据\130109藁城区冬小麦"
    ChinaSentinel2.ShapeFileToRaster(rasterfileapth, shapefilepath)
    return


def task_21():
    sentinel2rasterdirectory = r"E:\贾冰\sentinel2rastertrain\S2B_MSIL2A_T50SLH_20200417"
    savefolderpath = r"E:\贾冰\hebeisentinel2"
    # qhdm = r"130224"
    qhdm = r"130109"
    # qhdm = r"130631"
    tiffilenamelist = os.listdir(sentinel2rasterdirectory)
    for tiffilename in tiffilenamelist:
        [filename, extendname]= os.path.splitext(tiffilename)
        filenamelist = filename.split("_")
        print(filenamelist)
        newfilenamelist = [filenamelist[2], qhdm, filenamelist[3], filenamelist[-1]]
        newfilename = "_".join(newfilenamelist)
        newtiffilename = newfilename + extendname
        tiffilepath = os.path.join(sentinel2rasterdirectory, tiffilename)
        newtiffilepath = os.path.join(savefolderpath, newtiffilename)
        shutil.copyfile(tiffilepath, newtiffilepath)
    return


def task_22():
    rasterfilepath = r"E:\贾冰\sentinel2rastertrain\S2A_MSIL1C_T50SKH_20191114\S2A_MSIL1C_T50SKH_20191114_B01.tif"
    shapefilepath = r"F:\河北省冬小麦自动解译数据\130109藁城区冬小麦"
    newshapefilepath = r"E:\贾冰\newshapefiletransform\test"
    newlayername = r"test"
    rasterspatialreferencesystemwkt = RasterFile.GetSpatialreferencesystemwkt(rasterfilepath)
    ChinaSentinel2.ShapeFileTransformSpatialreferencesystem(
        rasterspatialreferencesystemwkt, shapefilepath, None, newshapefilepath, newlayername)
    return


def task_23():
    rasterfilepath = r"E:\贾冰\sentinel2rastertrain\S2A_MSIL1C_T50SKH_20191114\S2A_MSIL1C_T50SKH_20191114_B01.tif"
    shapefilepath = r"F:\河北省冬小麦自动解译数据\130109藁城区冬小麦"
    newshapefilepath = r"E:\贾冰\newshapefiletransform\rasterfiletest.tif"
    Sentinel2.ShapeFileToRasterFile(
        newshapefilepath, rasterfilepath, shapefilepath)
    return


def task_24():
    rasterfilepath = r"E:\贾冰\sentinel2rastertrain\S2A_MSIL1C_T50SKH_20191114\S2A_MSIL1C_T50SKH_20191114_B01.tif"
    project = RasterFile.GetRasterFileProjection(rasterfilepath)
    print(project)
    return


def task_25():
    rasterzipfilepath = r"E:\贾冰\河北冬小麦\sentinel2zipfile"
    saverasterdirectory = r"E:\贾冰\河北冬小麦\singlebandrasterfile"
    Sentinel2.BatchSentinel2RemoveRasterToSaveDirectory(rasterzipfilepath, saverasterdirectory)
    return


def task_26():
    trainsinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\trainsinglebandrasterfile"
    testsinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\testsinglebandrasterfile"
    allsinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\singlebandrasterfile"
    tainsentinel2zonelist = ["T50SLH", "T50SKH", "T50SLH", "T50SPJ"]
    testsentinel2zonelist = ["T50SKH", "T50SLH", "T50SMH", "T50SLJ", "T50SMJ"]
    singlebandrasterfilenamelist = os.listdir(allsinglebandrasterfolderpath)
    for singlebandrasterfilename in tqdm(singlebandrasterfilenamelist):
        filename = os.path.splitext(singlebandrasterfilename)[0]
        filenamestrlist = filename.split("_")
        tsentinel2zone = filenamestrlist[0]
        singlebandrasterfilepath = os.path.join(
            allsinglebandrasterfolderpath, singlebandrasterfilename)
        if tsentinel2zone in tainsentinel2zonelist:
            newsinglebandrasterfilepath = os.path.join(
                trainsinglebandrasterfolderpath, singlebandrasterfilename)
            shutil.copyfile(singlebandrasterfilepath, newsinglebandrasterfilepath)
        if tsentinel2zone in testsentinel2zonelist:
            testsinglebandrasterfilepath = os.path.join(
                testsinglebandrasterfolderpath, singlebandrasterfilename)
            shutil.copyfile(singlebandrasterfilepath, testsinglebandrasterfilepath)
    return


def task_27():
    trainsinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\trainsinglebandrasterfile"
    countycodesinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\countysinglebandrasterfile"
    countycodesentinel2dict = {"130109": ["T50SLH", "T50SKH"], "130224": ["T50SPJ"], "130631": ["T50SLH"]}
    singlebandrasterfilenamelist = os.listdir(trainsinglebandrasterfolderpath)
    for singlebandrasterfilename in tqdm(singlebandrasterfilenamelist):
        [filename, extend]= os.path.splitext(singlebandrasterfilename)
        filenamestrlist = filename.split("_")
        tsentinel2zone = filenamestrlist[0]
        singlebandrasterfilepath = os.path.join(
            trainsinglebandrasterfolderpath, singlebandrasterfilename)
        if tsentinel2zone in countycodesentinel2dict["130109"]:
            filenamestrlist_130109 = list(filenamestrlist)
            filenamestrlist_130109.insert(1, "130109")
            filename_130109 = "_".join(filenamestrlist_130109)
            singlebandrasterfilename_130109 = filename_130109 + extend
            singlebandrasterfilepath_130109 = os.path.join(
                countycodesinglebandrasterfolderpath, singlebandrasterfilename_130109)
            shutil.copyfile(singlebandrasterfilepath, singlebandrasterfilepath_130109)
        if tsentinel2zone in countycodesentinel2dict["130224"]:
            filenamestrlist_130224 = list(filenamestrlist)
            filenamestrlist_130224.insert(1, "130224")
            filename_130224 = "_".join(filenamestrlist_130224)
            singlebandrasterfilename_130224 = filename_130224 + extend
            singlebandrasterfilepath_130224 = os.path.join(
                countycodesinglebandrasterfolderpath, singlebandrasterfilename_130224)
            shutil.copyfile(singlebandrasterfilepath, singlebandrasterfilepath_130224)
        if tsentinel2zone in countycodesentinel2dict["130631"]:
            filenamestrlist_130631 = list(filenamestrlist)
            filenamestrlist_130631.insert(1, "130631")
            filename_130631 = "_".join(filenamestrlist_130631)
            singlebandrasterfilename_130631 = filename_130631 + extend
            singlebandrasterfilepath_130631 = os.path.join(
                countycodesinglebandrasterfolderpath, singlebandrasterfilename_130631)
            shutil.copyfile(singlebandrasterfilepath, singlebandrasterfilepath_130631)
    return


def task_28():
    rasterfolderpath = r"E:\贾冰\河北冬小麦\countysinglebandrasterfile"
    saverasterfolderpath = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile"
    rasterfilenamelist= os.listdir(rasterfolderpath)
    sentinel2zonenamelist = ["T50SLH", "T50SKH", "T50SPJ"]
    for rasterfilename in tqdm(rasterfilenamelist):
        filename = os.path.splitext(rasterfilename)[0]
        filenamestrlist = filename.split("_")
        sentinel2zonename = filenamestrlist[0]
        if sentinel2zonename in sentinel2zonenamelist:
            rasterfilepath = os.path.join(rasterfolderpath, rasterfilename)
            newrasterfilename = filenamestrlist[0] + r".tif"
            newrasterfilepath = os.path.join(saverasterfolderpath, newrasterfilename)
            shutil.copyfile(rasterfilepath, newrasterfilepath)
            sentinel2zonenamelist.remove(sentinel2zonename)
            continue
    return


def task_29():
    onlysentinel2zonerasterfolder = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile"
    shapefolderpath = r"E:\贾冰\河北冬小麦\DKshapefile"
    saveshapefolderpath = r"E:\贾冰\河北冬小麦\shapefiletransformspatialreferencesystem"
    shapefilenamelist = os.listdir(shapefolderpath)
    countycodesentinel2dict = {"130109": ["T50SLH", "T50SKH"], "130224": ["T50SPJ"], "130631": ["T50SLH"]}
    for shapefilename in tqdm(shapefilenamelist):
        countycode = shapefilename[:6]
        sentinel2zonelist = countycodesentinel2dict[countycode]
        shapefilepath = os.path.join(shapefolderpath, shapefilename)
        layername = ShapeFile.LayerIndexToLayerName(shapefilepath)
        for sentinel2zone in sentinel2zonelist:
            rasterfilename = sentinel2zone + r".tif"
            rasterfilepath = os.path.join(onlysentinel2zonerasterfolder, rasterfilename)
            spatialreferencesystemwkt = RasterFile.GetRasterFileSpatialreferencesystemwkt(rasterfilepath)
            newshapefilename = sentinel2zone + r"_" + shapefilename
            newshapefilepath = os.path.join(saveshapefolderpath, newshapefilename)
            ChinaSentinel2.ShapeFileTransformSpatialreferencesystem(
                spatialreferencesystemwkt, shapefilepath, layername, newshapefilepath, layername)
    return


def task_30():
    newrasterfolderpath = r"E:\贾冰\河北冬小麦\shapetoraster_bl"
    shapefolderpath = r"E:\贾冰\河北冬小麦\shapefiletransformspatialreferencesystem"
    onlysinglebandrasterfolderpath = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile"
    shapefilenamelist = os.listdir(shapefolderpath)
    for shapefilename in tqdm(shapefilenamelist):
        shapefilenamestrlist = shapefilename.split("_")
        sentinel2zone = shapefilenamestrlist[0]
        sentinel2zonerasterfilename = sentinel2zone + r".tif"
        sentinel2zonerasterfilepath = os.path.join(onlysinglebandrasterfolderpath, sentinel2zonerasterfilename)
        newrasterfilename = shapefilename + r"_bl.tif"
        newrasterfilepath = os.path.join(newrasterfolderpath, newrasterfilename)
        shapefilepath = os.path.join(shapefolderpath, shapefilename)
        ShapeToRasterFillFieldValue(newrasterfilepath, sentinel2zonerasterfilepath, shapefilepath)
    return


def task_31():
    rasterfilepath = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile\T50SLH.tif"
    shapefilepath = r"E:\贾冰\河北冬小麦\shapefiletransformspatialreferencesystem\T50SLH_130631望都县冬小麦"
    newrasterfilepath = r"E:\贾冰\河北冬小麦\shapetoraster_bl\test.tif"
    filename = r"BL"
    ShapeToRasterFillFieldValue(newrasterfilepath, rasterfilepath, shapefilepath, 0, filename)
    return


def task_32():
    shapefilepath = r"E:\贾冰\河北冬小麦\shapefiletransformspatialreferencesystem\T50SKH_130109藁城区冬小麦"
    filename = r"BL"
    datatypename = ShapeFile.GetFieldDataType(shapefilepath, None, filename)
    print(datatypename)
    return


def task_33():
    rasterfilepath = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile\T50SLH.tif"
    shapefilepath = r"E:\贾冰\河北冬小麦\xianjietransform\T50SLH_130631望都县区划"
    newrasterfilepath = r"E:\贾冰\河北冬小麦\xqshapetoraster\test.tif"
    ShapeFileToRasterFile(newrasterfilepath, rasterfilepath, shapefilepath)
    return


def task_34():
    """
      转换shapefile坐标系为栅格投影坐标系
      :return:
    """
    onlysentinel2zonerasterfolder = r"E:\贾冰\河北冬小麦\onlysentinel2rasterfile"
    shapefolderpath = r"E:\贾冰\河北冬小麦\qh"
    saveshapefolderpath = r"E:\贾冰\河北冬小麦\xianjietransform"
    shapefilenamelist = os.listdir(shapefolderpath)
    countycodesentinel2dict = {"130109": ["T50SLH", "T50SKH"], "130224": ["T50SPJ"], "130631": ["T50SLH"]}
    for shapefilename in shapefilenamelist:
        countycode = shapefilename[:6]
        sentinel2zonelist = countycodesentinel2dict[countycode]
        shapefilepath = os.path.join(shapefolderpath, shapefilename)
        layername = ShapeFile.LayerIndexToLayerName(shapefilepath)
        for sentinel2zone in sentinel2zonelist:
            rasterfilename = sentinel2zone + r".tif"
            rasterfilepath = os.path.join(onlysentinel2zonerasterfolder, rasterfilename)
            spatialreferencesystemwkt = RasterFile.GetRasterFileSpatialreferencesystemwkt(rasterfilepath)
            newshapefilename = sentinel2zone + r"_" + countycode
            newshapefilepath = os.path.join(saveshapefolderpath, newshapefilename)
            ShapeFileTransformSpatialreferencesystem(
                spatialreferencesystemwkt, shapefilepath, layername, newshapefilepath, layername)
    return


def task_35():
    newrasterfilepath = r"E:\贾冰\T50SLH_predict_shao.tif"
    shapeFilePath = r"E:\贾冰\T50SLH_predict_shao"
    rasterFilePath = r"E:\贾冰\T50SLH_predict_slk_skh.tif"
    ShapeFileToRasterFile(newrasterfilepath, rasterFilePath, shapeFilePath)
    return


def task_36():
    shapeFileFolderPath = r"E:\download\02_各县市分类矢量"
    saveFolderPath = r"E:\download\henansheng"
    # filePathList = TL.GetGoalFile(folderpath, ".shp")
    folderNameList = os.listdir(shapeFileFolderPath)
    for folderName in tqdm(folderNameList):
        shapeFilePath = os.path.join(shapeFileFolderPath, folderName)
        fileNameList = os.listdir(shapeFilePath)
        for fileName in fileNameList:
            filePath = os.path.join(shapeFilePath, fileName)
            savefilepath = os.path.join(saveFolderPath, fileName)
            shutil.copyfile(filePath, savefilepath)
    return


def task_37():
    shapefilepath = r""
    shapefilepath_2 = r""

    return


if __name__=="__main__":
    task_36()
